<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Código-Fonte de genetic_algorithm.h</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Gerado por Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Busca');
--></script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;Principal</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Arquivos</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <img id="MSearchSelect" src="search/search.png"
             onmouseover="return searchBox.OnSearchSelectShow()"
             onmouseout="return searchBox.OnSearchSelectHide()"
             alt=""/>
        <input type="text" id="MSearchField" value="Busca" accesskey="S"
             onfocus="searchBox.OnSearchFieldFocus(true)" 
             onblur="searchBox.OnSearchFieldFocus(false)" 
             onkeyup="searchBox.OnSearchFieldChange(event)"/>
        <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
        </div>
      </li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>Lista&nbsp;de&nbsp;Arquivos</span></a></li>
      <li><a href="globals.html"><span>Arquivos&nbsp;Membros</span></a></li>
    </ul>
  </div>
<h1>genetic_algorithm.h</h1><a href="genetic__algorithm_8h.html">Vá para a documentação deste arquivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef GENETIC_ALGORITHM_H</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define GENETIC_ALGORITHM_H</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &quot;<a class="code" href="population_8h.html">population.h</a>&quot;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &quot;<a class="code" href="genetic__operator_8h.html">genetic_operator.h</a>&quot;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &quot;<a class="code" href="selection__by__roulette_8h.html">selection_by_roulette.h</a>&quot;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="cross__over_8h.html">cross_over.h</a>&quot;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;mutate_bit_by_bit.h&quot;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &quot;<a class="code" href="definitions_8h.html">definitions.h</a>&quot;</span>
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00025"></a>00025 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00026"></a>00026 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty = GAtype,<span class="keyword">typename</span> _realTy = GAreal_type&gt; <span class="keyword">class </span><a class="code" href="classgenetic__algorithm.html">genetic_algorithm</a>;
<a name="l00027"></a>00027 
<a name="l00028"></a>00028 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00035"></a>00035 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00036"></a>00036 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00037"></a><a class="code" href="classgenetic__algorithm.html">00037</a> <span class="keyword">class </span><a class="code" href="classgenetic__algorithm.html">genetic_algorithm</a>
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039   <span class="keyword">public</span>:
<a name="l00040"></a>00040 
<a name="l00041"></a>00041     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00047"></a>00047     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00048"></a>00048     <a class="code" href="classgenetic__algorithm.html#af33b707193eeea4557c46ddb0bde22b6">genetic_algorithm</a> (<span class="keyword">const</span> <span class="keywordtype">int</span>&amp; max_generation = 1000);
<a name="l00049"></a>00049 
<a name="l00050"></a>00050     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00056"></a>00056     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00057"></a><a class="code" href="classgenetic__algorithm.html#ab125057b9774303bab463defd29000e1">00057</a>     <span class="keyword">const</span> <span class="keywordtype">int</span>&amp; <a class="code" href="classgenetic__algorithm.html#ab125057b9774303bab463defd29000e1">GetMaxGeneration</a> (<span class="keywordtype">void</span>)<span class="keyword"> const </span>{<span class="keywordflow">return</span> <a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a>;}
<a name="l00058"></a>00058 
<a name="l00059"></a>00059     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00065"></a>00065     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00066"></a><a class="code" href="classgenetic__algorithm.html#ad7e8c1a4ce92a617bf2d2a4496a4a31b">00066</a>     <span class="keywordtype">void</span> <a class="code" href="classgenetic__algorithm.html#ad7e8c1a4ce92a617bf2d2a4496a4a31b">SeMaxGeneration</a> (<span class="keyword">const</span> <span class="keywordtype">int</span>&amp; new_max) {<a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a> = new_max;}
<a name="l00067"></a>00067 
<a name="l00068"></a>00068     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00072"></a>00072     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00073"></a>00073     <span class="keywordtype">void</span> <a class="code" href="classgenetic__algorithm.html#a9a875d5695bfcc574b616f69e1605427">StartGA</a> (<span class="keywordtype">void</span>);
<a name="l00074"></a>00074 
<a name="l00075"></a>00075   <span class="keyword">protected</span>:
<a name="l00076"></a>00076 
<a name="l00077"></a>00077     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00081"></a>00081     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00082"></a><a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">00082</a>     <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;</a>* <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>;
<a name="l00083"></a>00083 
<a name="l00084"></a>00084     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00088"></a>00088     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00089"></a><a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">00089</a>     <span class="keywordtype">int</span> <a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a>;
<a name="l00090"></a>00090 
<a name="l00091"></a>00091     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00095"></a>00095     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00096"></a><a class="code" href="classgenetic__algorithm.html#a9f03558907645bff987053e1571c9816">00096</a>     <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;</a>* <a class="code" href="classgenetic__algorithm.html#a9f03558907645bff987053e1571c9816">_mutation</a>;
<a name="l00097"></a>00097 
<a name="l00098"></a>00098     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00102"></a>00102     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00103"></a><a class="code" href="classgenetic__algorithm.html#a4b1145baa8b987a6e69681f79a939b24">00103</a>     <a class="code" href="classcross__over.html">cross_over&lt;_ty,_realTy&gt;</a>* <a class="code" href="classgenetic__algorithm.html#a4b1145baa8b987a6e69681f79a939b24">_cross_over</a>;
<a name="l00104"></a>00104 
<a name="l00105"></a>00105     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00109"></a>00109     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00110"></a><a class="code" href="classgenetic__algorithm.html#a237298e9294747b7531beca4df11e049">00110</a>     <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;</a>* <a class="code" href="classgenetic__algorithm.html#a237298e9294747b7531beca4df11e049">_selection</a>;
<a name="l00111"></a>00111 };
<a name="l00112"></a>00112 
<a name="l00113"></a>00113 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00114"></a><a class="code" href="classgenetic__algorithm.html#af33b707193eeea4557c46ddb0bde22b6">00114</a> <a class="code" href="classgenetic__algorithm.html#af33b707193eeea4557c46ddb0bde22b6">genetic_algorithm&lt;_ty,_realTy&gt;::genetic_algorithm</a> (<span class="keyword">const</span> <span class="keywordtype">int</span>&amp; max_generation)
<a name="l00115"></a>00115 {
<a name="l00116"></a>00116   <a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a> = max_generation;
<a name="l00117"></a>00117   <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a> = <span class="keyword">new</span> <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;</a>();
<a name="l00118"></a>00118   <a class="code" href="classgenetic__algorithm.html#a9f03558907645bff987053e1571c9816">_mutation</a> = <span class="keyword">new</span> <a class="code" href="classmutate__bit__by__bit.html">mutate_bit_by_bit&lt;_ty,_realTy&gt;</a>(<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>);
<a name="l00119"></a>00119   <a class="code" href="classgenetic__algorithm.html#a4b1145baa8b987a6e69681f79a939b24">_cross_over</a> = <span class="keyword">new</span> <a class="code" href="classcross__over.html">cross_over&lt;_ty,_realTy&gt;</a>(<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>);
<a name="l00120"></a>00120   <a class="code" href="classgenetic__algorithm.html#a237298e9294747b7531beca4df11e049">_selection</a> = <span class="keyword">new</span> <a class="code" href="classselection__by__roulette.html">selection_by_roulette&lt;_ty,_realTy&gt;</a>(<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>);
<a name="l00121"></a>00121 }
<a name="l00122"></a>00122 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00123"></a><a class="code" href="classgenetic__algorithm.html#a9a875d5695bfcc574b616f69e1605427">00123</a> <span class="keywordtype">void</span> <a class="code" href="classgenetic__algorithm.html#a9a875d5695bfcc574b616f69e1605427">genetic_algorithm&lt;_ty,_realTy&gt;::StartGA</a> (<span class="keywordtype">void</span>)
<a name="l00124"></a>00124 {
<a name="l00125"></a>00125   <span class="keywordtype">int</span> iter = <a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a>;
<a name="l00126"></a>00126   <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;GeneratePopulation();<span class="comment">//Gera a população aleatória</span>
<a name="l00127"></a>00127   std::ofstream out(<span class="stringliteral">&quot;generation.ga&quot;</span>);
<a name="l00128"></a>00128   <span class="keywordflow">do</span>
<a name="l00129"></a>00129   {
<a name="l00130"></a>00130 
<a name="l00131"></a>00131     <span class="comment">//Calcula o valor da função objetivo com as novas coordenadas(preparação para a seleção)</span>
<a name="l00132"></a>00132     <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;SetIndividualsValue();
<a name="l00133"></a>00133     <span class="keywordflow">if</span>(iter==<a class="code" href="classgenetic__algorithm.html#aa27789d88ad571a15a9ad984f238f51e">_max_generation</a>)
<a name="l00134"></a>00134     {
<a name="l00135"></a>00135 
<a name="l00136"></a>00136       std::cout &lt;&lt; std::endl &lt;&lt; *(<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;GetBestId()) &lt;&lt; std::endl;
<a name="l00137"></a>00137       <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;GetBestId()-&gt;PrintRealPosition();
<a name="l00138"></a>00138     }
<a name="l00139"></a>00139     
<a name="l00140"></a>00140     <span class="comment">//Grava os dados da nova geração</span>
<a name="l00141"></a>00141     <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;SendGenerationDataToStream(out);
<a name="l00142"></a>00142 
<a name="l00143"></a>00143     <span class="comment">//Aplica o operador de seleção</span>
<a name="l00144"></a>00144     <a class="code" href="classgenetic__algorithm.html#a237298e9294747b7531beca4df11e049">_selection</a>-&gt;doApplyGeneticOperator();
<a name="l00145"></a>00145 
<a name="l00146"></a>00146     <span class="comment">//Cria os pares de indivíduos, e aplica o operador de cruzamento</span>
<a name="l00147"></a>00147     <a class="code" href="classgenetic__algorithm.html#a4b1145baa8b987a6e69681f79a939b24">_cross_over</a>-&gt;MakePairs();
<a name="l00148"></a>00148     <a class="code" href="classgenetic__algorithm.html#a4b1145baa8b987a6e69681f79a939b24">_cross_over</a>-&gt;doApplyGeneticOperator();
<a name="l00149"></a>00149 
<a name="l00150"></a>00150     <span class="comment">//Finalmente aplica o operador de mutação</span>
<a name="l00151"></a>00151     <a class="code" href="classgenetic__algorithm.html#a9f03558907645bff987053e1571c9816">_mutation</a>-&gt;doApplyGeneticOperator();
<a name="l00152"></a>00152 
<a name="l00153"></a>00153   } <span class="keywordflow">while</span> (--iter);
<a name="l00154"></a>00154 
<a name="l00155"></a>00155   <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;SetIndividualsValue();
<a name="l00156"></a>00156   std::cout &lt;&lt; std::endl &lt;&lt; *<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a> &lt;&lt; std::endl;
<a name="l00157"></a>00157   std::cout &lt;&lt; std::endl &lt;&lt; *(<a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;GetBestId()) &lt;&lt; std::endl;
<a name="l00158"></a>00158   <a class="code" href="classgenetic__algorithm.html#a1b2109867086541f375f091a34b1413b">_population</a>-&gt;GetBestId()-&gt;PrintRealPosition();
<a name="l00159"></a>00159   
<a name="l00160"></a>00160 }
<a name="l00161"></a>00161 
<a name="l00162"></a>00162 
<a name="l00163"></a>00163 <span class="preprocessor">#endif //GENETIC_ALGORITHM_H</span>
</pre></div></div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>Todos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Arquivos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Funções</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variáveis</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Definições de Tipos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Amigas</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address style="text-align: right;"><small>Gerado em Sat Oct 30 13:47:50 2010 por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>
